<html>
<head><meta charset="utf-8"><title>data-type-layout · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html">data-type-layout</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="131162274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/131162274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#131162274">(Aug 09 2018 at 10:09)</a>:</h4>
<p>Spinning off from <span class="user-mention" data-user-id="120791">@RalfJ</span>'s comments in data-invariants, I do want to follow-up on <span class="user-mention" data-user-id="123893">@alercah</span>'s question here:</p>
<blockquote>
<p>is it okay to store a repr(rust) value on disk and read it back out to memory? do we offer guarantees that this works across identically defined types? across different compiler versions? can the compiler randomly reassign discriminants for enums every time the program runs?</p>
</blockquote>
<p>I don't have an answer specifically to it, but I think it would be really great to try and make some progress on trying to document what things user's can rely on when it comes to memory layout. I think a good start would be to try and enumerate the <em>questions</em> even and write them down -- ideally in a commit to <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/">the rust-lang-rfcs repo</a>. (Maybe along with some notes about the pros/cons) </p>
<p><span class="user-mention" data-user-id="123893">@alercah</span> are you interested in maybe taking point on that? </p>
<p>I'd like to organize an initial "focused meeting" around this topic but I'm not sure how that should work best. </p>
<p>(Another related question: "if two <code>repr(rust)</code> structs have the same fields of the same type, do they have the same layout? <code>repr(C)</code>?")</p>



<a name="132007991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132007991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132007991">(Aug 12 2018 at 19:15)</a>:</h4>
<p>I'm not sure I have the ability to make commitments right now, but as long as I'm interested I can keep thinking on this. ;) <code>repr(C)</code> should have an explicitly defined ABI, so that it can be safely used with FFI in both directions. This may in some cases just be a reference to the platform ABI, but we want to ensure that e.g. struct padding is guaranteed to work as expected. It would also make it possible for users to understand when they can safely read bytes into <code>repr(C)</code>, valuable for any binary protocol.</p>



<a name="132008249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132008249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132008249">(Aug 12 2018 at 19:23)</a>:</h4>
<p><code>repr(rust)</code>, on the other hand, I can see reasons to even forbid it on types with the same layout in the same program. Especially for types with private fields, the compiler could conceivably try to optimize the most frequently accessed field into offset 0, and this may vary from type to type.</p>



<a name="132008398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132008398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132008398">(Aug 12 2018 at 19:28)</a>:</h4>
<p>And that means we give the least guarantee for now, so I'm happy to propose this as our guideline for now---if you want reliable layout, you could always use <code>repr(C)</code>. We could also consider adding a <code>repr(stable)</code> which is guaranteed to be the same for identical definitions, even across platforms where the C ABI may vary.</p>



<a name="132008472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132008472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132008472">(Aug 12 2018 at 19:31)</a>:</h4>
<p>(Since <code>repr(C)</code> enum layout is currently fully specified in a way that should be platform-invariant I think, perhaps we should check that it is actually compatible with C ABI enums on all/most platforms and see if it makes sense to retract that guarantee slightly? Unsure here)</p>



<a name="132597914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132597914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132597914">(Aug 22 2018 at 19:42)</a>:</h4>
<p>Fully agreed with all of that, <span class="user-mention" data-user-id="123893">@alercah</span> :D</p>



<a name="132633178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132633178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132633178">(Aug 23 2018 at 11:57)</a>:</h4>
<p>So I want to talk about the mechanics of running this conversation. </p>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> had the idea, which I like, of opening up various issues on <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/">https://github.com/rust-rfcs/unsafe-code-guidelines/</a>, devoted to various type constructors.</p>
<p>wycats had the idea, which I also like, of writing out a lot of things that we believe to be uniformly agreed upon, and using that to try to find the contours of the rules. e.g., I believe that <code>Option&lt;&amp;T&gt;</code> should be represented always a pointer (thin or fat). The idea here is that there are individual questions which have (maybe?) known answers, but if we go from those to general principles things get murky.</p>
<p>What both of these say to me is that we basically need some useful "seeds" for discussion.</p>
<p>I think there are a few questions I would like to be able to answer:</p>
<ul>
<li>Where applicable, what are the <code>#[repr]</code> options and what can we say with certainty?</li>
<li>Around enums in particular, what are the rules regarding "niche optimizations" and the layout of <code>Option&lt;T&gt;</code>?<ul>
<li>Does this e.g. apply to other enums (in terms of guarantees)</li>
</ul>
</li>
<li>Safety/Validity invariants -- can we write them out "semi-formally" (<span class="user-mention" data-user-id="120791">@RalfJ</span>'s blog post has a good start, of course)</li>
</ul>



<a name="132647853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132647853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132647853">(Aug 23 2018 at 16:37)</a>:</h4>
<p>Related question: Is <code>bool</code> guaranteed to be laid out as bit-patterns 0 and 1 for true and false? Is its size guaranteed?</p>



<a name="132647938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132647938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132647938">(Aug 23 2018 at 16:39)</a>:</h4>
<p>I think my personal inclination is to say that no-<code>repr</code> and <code>repr(Rust)</code> are always equivalent and always unspecified. Any additional guarantees (e.g. <code>Option&lt;&amp;T&gt;</code>) should be promised by adding an attribute which guarantees a specific layout.</p>



<a name="132648113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132648113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132648113">(Aug 23 2018 at 16:42)</a>:</h4>
<p>This means that someone can write an isomorphic enum and opt into the promised layout, but we don't have to make any general promises. This would in turn imply to me that we should probably at least start by minimizing commitment. I think we should promise that 1-element tuples, including newtype structs but not including enum variants, are laid out as the underlying type.</p>



<a name="132648130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132648130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kennytm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132648130">(Aug 23 2018 at 16:43)</a>:</h4>
<blockquote>
<p>Is bool guaranteed to be laid out as bit-patterns 0 and 1 for true and false? Is its size guaranteed?</p>
</blockquote>
<p>oh no not <a href="https://github.com/rust-lang/rust/issues/46156" target="_blank" title="https://github.com/rust-lang/rust/issues/46156">#46156</a> / <a href="https://github.com/rust-lang/rust/issues/46176" target="_blank" title="https://github.com/rust-lang/rust/issues/46176">#46176</a> again</p>



<a name="132648575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132648575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132648575">(Aug 23 2018 at 16:51)</a>:</h4>
<p>Ok, so looks like we do want to promise 0 and 1. :) It seems to me that C allows you to put a 2 into a <code>_Bool</code>, whereas Rust does not?</p>



<a name="132648742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132648742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132648742">(Aug 23 2018 at 16:54)</a>:</h4>
<p>Ahhh. Looks like it's even better. It's unspecified whether or not it's UB.</p>



<a name="132648752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132648752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132648752">(Aug 23 2018 at 16:55)</a>:</h4>
<p>It might not be.</p>



<a name="132648769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132648769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132648769">(Aug 23 2018 at 16:55)</a>:</h4>
<p>But the compiler isn't required to document whether or not it's UB. <span class="emoji emoji-1f923" title="rofl">:rofl:</span></p>



<a name="132649722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132649722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132649722">(Aug 23 2018 at 17:15)</a>:</h4>
<p>That's not how it works, to be clear</p>



<a name="132649789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132649789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132649789">(Aug 23 2018 at 17:16)</a>:</h4>
<p>There are two values of type <code>_Bool</code>; <code>0</code> and <code>1</code>. An object of type <code>_Bool</code> must have one of these values, or else it is UB. It is unspecified what the bit patterns of these values are, however common implementations use <code>0b00000000</code> and <code>0b00000001</code>.</p>



<a name="132649866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132649866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132649866">(Aug 23 2018 at 17:18)</a>:</h4>
<p><span class="user-mention" data-user-id="123893">@alercah</span> it is guaranteed that <code>enum { V1(reference), V2 }</code> and <code>enum { V1, V2(reference) }</code> are ABI-compatible with pointers</p>



<a name="132649890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132649890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132649890">(Aug 23 2018 at 17:19)</a>:</h4>
<p>I don't believe it's guaranteed that <code>struct { T }</code> is ABI-compatible with <code>T</code>; that's what <code>#[repr(transparent)]</code> is for</p>



<a name="132649911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132649911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132649911">(Aug 23 2018 at 17:19)</a>:</h4>
<p>The C standard requires that 0 integer values have the all-0s bit pattern. But it says that using an out-of-range integer value is UB only if it's a trap representation, and the existence of trap representations is unspecified.</p>
<p>And yeah, I forgot about <code>repr(transparent)</code>.</p>



<a name="132649990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132649990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132649990">(Aug 23 2018 at 17:21)</a>:</h4>
<p>So <code>0b00000000</code> is guaranteed to be <code>_False</code>; <code>0b00000001</code> may be a trap, may be <code>_True</code>, or may just cause other unspecified weirdness (e.g. it might be true in conditionals but not behave like <code>_True</code> does in bitwise operations).</p>



<a name="132650177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650177">(Aug 23 2018 at 17:24)</a>:</h4>
<p>Oh wait. hm. that's not <em>quite</em> right, now with the new 2s-complement rules.</p>



<a name="132650206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650206">(Aug 23 2018 at 17:25)</a>:</h4>
<p>that's not true - by 6.2.6/5, "Certain object representations need not represent a value of the object type.  If the stored value of an object has such a representation and is read by an lvalue expression that does not  have  character  type,  the  behavior  is  undefined."</p>



<a name="132650256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650256">(Aug 23 2018 at 17:26)</a>:</h4>
<p>Yes, but that doesn't imply uniqueness of values. In particular, see 6.2.6.2/1 and 6.2.6.2/5.</p>



<a name="132650264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650264">(Aug 23 2018 at 17:26)</a>:</h4>
<p>basically, if you store a bitpattern that is not either <code>_Bool(0)</code> or <code>_Bool(1)</code> into memory, and then read it out as a <code>_Bool</code>, you get undefined behavior</p>



<a name="132650301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650301">(Aug 23 2018 at 17:27)</a>:</h4>
<p>So <code>_Bool</code> is guarantee to have one bit, somewhere, which determines if it is 0 or 1. It's not guaranteed to be the low bit. It is unspecified whether any combination of the other bits forms a trap representation, but if they do not, only the governing bit matters.</p>



<a name="132650352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650352">(Aug 23 2018 at 17:28)</a>:</h4>
<p>Additionally, all-zeroes is guaranteed to be a valid 0 representation. But e.g. the only valid 1 representation might be all-1s.</p>



<a name="132650374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650374">(Aug 23 2018 at 17:28)</a>:</h4>
<p>ah, I see what you're saying. Yeah</p>



<a name="132650393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650393">(Aug 23 2018 at 17:29)</a>:</h4>
<p>hm the draft I'm looking at predates the 2s complement rule though. Let me see if I can find that one.</p>



<a name="132650394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650394">(Aug 23 2018 at 17:29)</a>:</h4>
<p>However, I'd rather have <code>bool</code> be implementation defined, because compat with <code>_Bool</code> is more important, imo, than compat with <code>u8</code></p>



<a name="132650463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650463">(Aug 23 2018 at 17:30)</a>:</h4>
<p>and if you assume compat with <code>u8</code>, you should just stick to normal platforms</p>



<a name="132650490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650490">(Aug 23 2018 at 17:31)</a>:</h4>
<blockquote>
<p>I think my personal inclination is to say that no-<code>repr</code> and <code>repr(Rust)</code> are always equivalent and always unspecified. Any additional guarantees (e.g. <code>Option&lt;&amp;T&gt;</code>) should be promised by adding an attribute which guarantees a specific layout.</p>
</blockquote>
<p>I feel this is too strong and impractical, but we can have it out "on the issue tracker" =) It comes down to similar reasoning from <code>bool</code> land, though.</p>



<a name="132650557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650557">(Aug 23 2018 at 17:32)</a>:</h4>
<p>What's too strong and impractical about it? I feel like <code>repr(Rust)</code> and no-<code>repr</code> are naturally the same thing, i.e., "ABI doesn't matter"</p>



<a name="132650579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132650579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132650579">(Aug 23 2018 at 17:33)</a>:</h4>
<p>we've already promised <code>Option&lt;&amp;T&gt;</code>, tho, so that definitely can't be rolled back (and why would we want to?)</p>



<a name="132651499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651499">(Aug 23 2018 at 17:49)</a>:</h4>
<p>I was specifically referring to things lke "how <code>&amp;T</code> is represented"</p>



<a name="132651502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651502">(Aug 23 2018 at 17:49)</a>:</h4>
<p>and <code>Option&lt;&amp;T&gt;</code></p>



<a name="132651525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651525">(Aug 23 2018 at 17:50)</a>:</h4>
<p>not the idea of "say nothing" and <code>#[repr(rust)]</code> being equivalent, which I totally agree with</p>



<a name="132651573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651573">(Aug 23 2018 at 17:50)</a>:</h4>
<p>but basically I don't think we can say "<code>#[repr(rust)]</code> means totally impl defined"</p>



<a name="132651574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651574">(Aug 23 2018 at 17:50)</a>:</h4>
<p>Do we actually guarantee that all isomorphic-to-<code>Option</code> enums are laid out that way? Or is it only <code>Option</code> itself?</p>



<a name="132651588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651588">(Aug 23 2018 at 17:50)</a>:</h4>
<p><em>that</em> is an interesting question that I wanted to highlight for later :)</p>



<a name="132651616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651616">(Aug 23 2018 at 17:51)</a>:</h4>
<p>(however, I'd probably be willing to make the "isomorphic" guarantee)</p>



<a name="132651631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651631">(Aug 23 2018 at 17:51)</a>:</h4>
<p>(but I think saying <code>Option</code> might <em>suffice</em>)</p>



<a name="132651637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651637">(Aug 23 2018 at 17:51)</a>:</h4>
<p>(at least to start)</p>



<a name="132651639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651639">(Aug 23 2018 at 17:51)</a>:</h4>
<p>I think making some guarantees for primitives is ok, because we don't currently have a mechanism to define "new" ones. For user-defined types, including <code>Option</code>, it may make more sense to move the guarantees to <code>repr</code>.</p>



<a name="132651654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651654">(Aug 23 2018 at 17:51)</a>:</h4>
<p>yes, I could see that perhaps one declares a specific repr (and option declares that)</p>



<a name="132651764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651764">(Aug 23 2018 at 17:53)</a>:</h4>
<p>I thought <code>#[repr(rust)]</code>doesn't exist.</p>



<a name="132651872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651872">(Aug 23 2018 at 17:55)</a>:</h4>
<p>heh I don't remember but it seems like it would mean whatever "the default is" if it did ;)</p>



<a name="132651919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651919">(Aug 23 2018 at 17:56)</a>:</h4>
<p>"The repr formerly known as <code>rust</code>"</p>



<a name="132651951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651951">(Aug 23 2018 at 17:56)</a>:</h4>
<p><code>repr(rust)</code> would be useful if we let you tag an entire module/block with <code>repr(C)</code> to use as a default</p>



<a name="132651974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132651974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132651974">(Aug 23 2018 at 17:57)</a>:</h4>
<blockquote>
<p>(but I think saying <code>Option</code> might <em>suffice</em>)</p>
</blockquote>
<p>As a casual observer, I think it's a lot nicer if it is the isomorphic enums. I know nothing of the tradeoffs, so I'm mostly thinking from the teaching side of it.</p>



<a name="132652181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132652181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132652181">(Aug 23 2018 at 18:01)</a>:</h4>
<p>the nomicon definitely specifies "isomorphic to option"</p>
<p>at least, I'm pretty sure it did at one time...</p>



<a name="132652359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132652359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132652359">(Aug 23 2018 at 18:05)</a>:</h4>
<p>ah, here we go:</p>
<blockquote>
<p>As a special case, an enum is eligible for the "nullable pointer optimization" if it contains exactly two variants, one of which contains no data and the other contains a field of one of the non-nullable types listed above. This means no extra space is required for a discriminant; rather, the empty variant is represented by putting a null value into the non-nullable field. This is called an "optimization", but unlike other optimizations it is guaranteed to apply to eligible types.</p>
</blockquote>



<a name="132652531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132652531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132652531">(Aug 23 2018 at 18:08)</a>:</h4>
<p>FWIW, I think we currently do more than that, but I might be wrong -- e.g. using extra space in <code>bool</code> etc</p>



<a name="132652608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132652608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132652608">(Aug 23 2018 at 18:10)</a>:</h4>
<p>yeah, those are optimizations; NPO is a guarantee</p>



<a name="132652728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132652728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132652728">(Aug 23 2018 at 18:13)</a>:</h4>
<p>so basically I think these things can be separated:</p>



<a name="132652734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132652734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132652734">(Aug 23 2018 at 18:14)</a>:</h4>
<p>there is the concept of a niche, and then there which types "offer" those niches</p>



<a name="132652777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132652777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132652777">(Aug 23 2018 at 18:14)</a>:</h4>
<p>(I think that's the terminology <span class="user-mention" data-user-id="119009">@eddyb</span> used)</p>



<a name="132652883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132652883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132652883">(Aug 23 2018 at 18:16)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> what do you mean?</p>



<a name="132653246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132653246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132653246">(Aug 23 2018 at 18:23)</a>:</h4>
<p>a "niche" is a place to "store" a 1-bit discriminant; some types have them, some types don't</p>



<a name="132653255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132653255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132653255">(Aug 23 2018 at 18:23)</a>:</h4>
<p>essentially, it means "some invalid bit pattern"</p>



<a name="132653263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132653263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132653263">(Aug 23 2018 at 18:23)</a>:</h4>
<p>so we might guarantee that <code>&amp;T</code> has a niche, but not say one way or the other about <code>bool</code>, etc</p>



<a name="132653271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132653271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132653271">(Aug 23 2018 at 18:23)</a>:</h4>
<p>I guess it's no different than what you were saying</p>



<a name="132653274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132653274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132653274">(Aug 23 2018 at 18:23)</a>:</h4>
<p>just saying you can kind of break it down into a property of types</p>



<a name="132653328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132653328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132653328">(Aug 23 2018 at 18:24)</a>:</h4>
<p>(obviously one can also generalize the concept beyond 1 bit; I don't believe rustc does today)</p>



<a name="132653334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132653334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132653334">(Aug 23 2018 at 18:24)</a>:</h4>
<p>ah, sure! I'm just speaking from a "standardization"/ABI-guarantee standpoint for FFI, basically.</p>



<a name="132659678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132659678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132659678">(Aug 23 2018 at 20:45)</a>:</h4>
<blockquote>
<p>we've already promised Option&lt;&amp;T&gt;, </p>
</blockquote>
<p>we have promised <code>Option</code> but we have not promised the same for when you define your own <code>Option</code></p>



<a name="132659712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132659712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132659712">(Aug 23 2018 at 20:45)</a>:</h4>
<p>hm seems the nomicon did <em>oops</em>^^</p>



<a name="132659765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132659765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132659765">(Aug 23 2018 at 20:46)</a>:</h4>
<p>people have brought up that different isomorphic structs might get different layout based on PGO</p>



<a name="132659775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132659775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132659775">(Aug 23 2018 at 20:46)</a>:</h4>
<p>so we might not want to commit too strongly on that^^</p>



<a name="132659792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132659792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132659792">(Aug 23 2018 at 20:46)</a>:</h4>
<p>if we say "isomorphic to option", does that include <code>type MyOption&lt;T&gt; = Result&lt;T, ()&gt;</code>?</p>



<a name="132661693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132661693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132661693">(Aug 23 2018 at 21:26)</a>:</h4>
<p>Maybe! :P</p>



<a name="132704470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout/near/132704470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/data-type-layout.html#132704470">(Aug 24 2018 at 16:18)</a>:</h4>
<p>no - it says specifically <code>enum { V1, V2(non-nullable-pointer) }</code> or <code>enum { V1(non-nullable-pointer), V2 }</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>